<?php
use Common\Common\GlobalParam;

function fc_clear_all()
{
    S('user_list', NULL);
    S('department_list', NULL);
}

function fc_get_user_list()
{
    $data = S('user_list');
    if (empty($data)) {
        $model = D('sys_user');
        $data = S('user_list');
        if (empty($data)) {
            $data = $model->where('state>=0 and role!=99')->select();
        }
        S('user_list', $data, 86400);
    }
    return $data;
}

function fc_get_user($user_id)
{
    $data = fc_get_user_list();
    foreach ($data as $user) {
        if ($user['id'] == $user_id) {
            return $user;
        }
    }
    return null;
}

function fc_get_user_name($user_id)
{
    $data = fc_get_user($user_id);
    if (!empty($data)) {
        return $data['name'];
    }
    return '';
}

function fc_get_department_list()
{
    $data = S('department_list');
    if (empty($data)) {
        $model = D('SysDepartment');
        $data = S('department_list');
        if (empty($data)) {
            $data = $model->get_list();
        }
        S('department_list', $data, 86400);
    }
    return $data;
}

function fc_get_department_tree()
{
    $data = fc_get_department_list();
    $data = list_to_tree($data);
    return $data;
}

function fc_get_department($department_id)
{
    $data = fc_get_department_list();
    foreach ($data as $v) {
        if ($v['id'] == $department_id) {
            return $v;
        }
    }
    return null;
}

function fc_search_department_from_tree($departs, $department_id)
{
    if (count($departs) > 0) {
        foreach ($departs as $depart) {
            if ($depart['id'] == $department_id) {
                return $depart;
            }

            if (count($depart['_child']) > 0) {
                $result = fc_search_department_from_tree($depart['_child'], $department_id);
                if ($result != null) {
                    return $result;
                }
            }
        }
    }
    return null;
}

function fc_get_department_from_tree($department_id)
{
    $departs = fc_get_department_tree();
    return fc_search_department_from_tree($departs, $department_id);
}

//重置当月统计数据的临时表
function fc_reset_temp_statistics()
{
    $crm_custom = M('crm_custom');

    $condition = array();
    $condition['state'] = GlobalParam::$Custom_State_Distributed;
    $follow_count = $crm_custom->where($condition)->field('leader_user_id as uid,count(leader_user_id) as count_value')->group('leader_user_id')->select();

    $condition = array();
    $condition['sign_time'] = array(array('egt', get_month_first_day()), array('lt', get_next_month_first_day(date('Y-m-01', time()))));
    $condition['state'] = GlobalParam::$Custom_State_Signed;
    $signed_count = $crm_custom->where($condition)->field('sign_user_id as uid,count(sign_user_id) as count_value')->group('sign_user_id')->select();

    $condition = array();
    $condition['create_time'] = array(array('egt', get_month_first_day()), array('lt', get_next_month_first_day(date('Y-m-01', time()))));
    $condition['state'] = array('egt', GlobalParam::$Custom_State_Undistributed);
    $add_count = $crm_custom->where($condition)->field('add_user_id as uid,count(add_user_id) as count_value')->group('add_user_id')->select();

    $crm_activity = M('crm_activity');
    $condition = array();
    $condition['create_time'] = array(array('egt', get_month_first_day()), array('lt', get_next_month_first_day(date('Y-m-01', time()))));
    $condition['type'] = array('eq', 1);
    $condition['state'] = array('eq', 0);
    $phone_count = $crm_activity->where($condition)->field('add_user_id as uid,count(add_user_id) as count_value')->group('add_user_id')->select();

    $users = fc_get_user_list();
    $depart_count = [];
    foreach ($users as $user) {
        $user_id = $user['id'];
        $row = [];
        $row['user_id'] = $user_id;
        $ufc = searchArray($follow_count, 'uid', $user_id);
        if (!empty($ufc)) {
            $row['follow_count'] = $ufc['count_value'];
        } else {
            $row['follow_count'] = 0;
        }
        $ufc = searchArray($signed_count, 'uid', $user_id);
        if (!empty($ufc)) {
            $row['signed_count'] = $ufc['count_value'];
        } else {
            $row['signed_count'] = 0;
        }
        $ufc = searchArray($add_count, 'uid', $user_id);
        if (!empty($ufc)) {
            $row['add_count'] = $ufc['count_value'];
        } else {
            $row['add_count'] = 0;
        }
        $ufc = searchArray($phone_count, 'uid', $user_id);
        if (!empty($ufc)) {
            $row['phone_count'] = $ufc['count_value'];
        } else {
            $row['phone_count'] = 0;
        }
        $depart_count[] = $row;
    }

    $temp = M('temp_statistics');
    $temp->execute('truncate temp_statistics');
    $temp->addAll($depart_count);
}

function fc_get_temp_statistics_by_depart_id($depart_id)
{
    $depart = fc_get_department_from_tree($depart_id);
    $user_ids = fc_get_employee_ids($depart);
    $temp = M('temp_statistics');
    $condition = array();

    if (!empty($user_ids)) {
        $condition['user_id'] = array('in', $user_ids);
        $result = $temp->where($condition)->select();
        return $result;
    }
    return [];
}

function fc_set_temp_statistics($user_id, $key, $value, $isInc = true)
{
    try {
        $temp = M('temp_statistics');
//        $row = $temp->find('user_id=%d');
//        if (empty($row)) {
//            $data['user_id'] = $user_id;
//            $data['follow_count'] = 0;
//            $data['signed_count'] = 0;
//            $data['add_count'] = 0;
//            $data['phone_count'] = 0;
//            $temp->create();
//        }
        if ($isInc) {
            $temp->where('user_id=%d', $user_id)->setInc($key, $value);
        } else {
            $temp->where('user_id=%d', $user_id)->setDec($key, $value);
        }
    } catch (Exception $ex) {

    }
}